extends KinematicBody2D
class_name KDemo

# 1 超右 -1 超左
var knightFace : int = 1; 

var x:float = 200;
var y:float = 200;
# 速度标量
var speed = 200;
# 速度向量
var velocity = Vector2(1,1);

func _init():
	# 创建碰撞体
	self.position = Vector2(x,y)

	# 外观
	var knight = Sprite.new();
	knight.texture = load("res://icon.png")

	self.add_child(knight);

	# 实际大小
	var h = knight.texture.get_height() / 2;
	var w = knight.texture.get_width() / 2;

	# 碰撞盒
	var rect = RectangleShape2D.new();
	rect.extents = Vector2(w,h);
	var oid = self.create_shape_owner(self);
	self.shape_owner_add_shape(oid,rect);
	print("radius",rect.extents);
	

func _physics_process(delta):
	# 生成速度
	handle_input();

	# print(velocity,velocity*delta);
	# 移动并检测碰撞
	var col:KinematicCollision2D = move_and_collide(velocity * delta)
	# if col:
	# 	print(OS.get_ticks_usec(),"col",col.collider.name);
	# else:
	# 	print("no")

func handle_input():
	velocity = Vector2()
	if Input.is_action_pressed('ui_right'):
		velocity.x += 1
	if Input.is_action_pressed('ui_left'):
		velocity.x -= 1
	if Input.is_action_pressed('ui_down'):
		velocity.y += 1
	if Input.is_action_pressed('ui_up'):
		velocity.y -= 1
	velocity = velocity.normalized() * speed
	
